package com.project.service.impl;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public abstract class BaseService {
    /*会话工厂*/
    private static SqlSessionFactory factory;

    static {
        try {
            //读取mybatis主配置文件
            Reader r = Resources.getResourceAsReader("mybatis.cfg.xml");
            //创建会话工厂
            factory = new SqlSessionFactoryBuilder().build(r);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取会话
     * @return 会话对象
     */
    public SqlSession getSession (){
        return  factory.openSession();
    }
}
